﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using Ivy.SiteAdmin.BusinessLogic;
using Ivy.SiteAdmin.IBusinessLogic;
using Ivy.SiteAdmin.Entity;

using Ivy.Common.Framework.GeneralUtility;
using DasIdeen.UserControls;
using DasIdeen.ViewEntity;

namespace DasIdeen.Pages
{
    public partial class ContentListing : System.Web.UI.Page
    {

        #region  Variable & constant
        //All constants and local variable should be declared here
        //const string constContentTypeCode = "DNL";
        string lContentTypeCode = string.Empty;
        #endregion  Variable & constant

        #region Constructor & Destructor
        //Generally not needed in UI layer
        #endregion Constructor & Destructor

        #region Properties
        //UI layer may have public custom properties. these are very handy for User Controls
        #endregion Properties

        #region Public Methods
        //UI layer may have public custom properties. these are very handy for User Controls
        public string ContentTypeCode
        {
            get
            {
                return lContentTypeCode;
            }
            set
            {
                lContentTypeCode = value;
            }
        }
        #endregion Public Methods

        #region Events

        protected void Page_Load(object sender, EventArgs e)
        {
            try //All events in pages or user control should have try{} catch {} block.
            {
                //First check if the current logged in user is having proper role/authorization to access this page or not.
                if (IsUserAutherization())
                {
                    //TODO - Your code to display the initial layout/data etc.
                    InitializeScreen();
                }
                else
                {
                    //TODO - Your code to redirect page to "NOT AUTHORIZED" page or any other page as required by the business logic.
                }
            }
            catch (Exception ex)
            {
                ExceptionUtility.ExceptionHandler(ex, Server.MapPath("~/"));
            }
        }

        protected void btnCreateNewContent_Click1(object sender, EventArgs e)
        {
             Response.Redirect("~/Pages/ContentDetails.aspx?ContentTypeCode=" + this.ContentTypeCode);
        }

        protected void RptContentSummaryControl_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            //PLEASE CHECK THE LINK http://stackoverflow.com/questions/2496856/asp-net-loading-a-user-control-in-a-repeater
            try //All events in pages or user control should have try{} catch {} block.
            {
                if (e.Item.DataItem != null)
                {
                    ContentSummaryControl lContentSummaryControl = (ContentSummaryControl)e.Item.FindControl("ContentSummaryCtrl");
                    ContentStatistics lContentStatistics = (ContentStatistics)e.Item.DataItem;
                    lContentSummaryControl.ContentStatisticsEntity = lContentStatistics;
                    lContentSummaryControl.DataBind(lContentStatistics);

                }

            }
            catch (Exception ex)
            {
                ExceptionUtility.ExceptionHandler(ex, Server.MapPath("~/"));
            }
        }

        #endregion Events

        #region Private Methods

        /// <summary>
        /// This method is to check if the current logged in user is having proper role/authorization to access this page or not.
        /// </summary>
        private bool IsUserAutherization()
        {
            bool _IsUserAutherization = false;
            //TODO - Your code to check authorization goes here.

            //for the time being let us allow the user to access this page.
            //but remove the below line once you have a proper logic.
            _IsUserAutherization = true;

            return _IsUserAutherization;
        }

        /// <summary>
        /// This method is to clear all UI controls which are data binded.
        /// </summary>
        private void ClearScreen()
        {
        }

        /// <summary>
        /// This method is to bind all the UI control like dropdown list or list box etc with the required initial data, 
        /// </summary>
        private void FillLoadLookUpData()
        {
        }

        /// <summary>
        /// This method is to load the UI with initial set of data based of the business logic.
        /// </summary>
        private void InitializeScreen()
        {
            ClearScreen();
            FillLoadLookUpData();
            this.ContentTypeCode = Request.QueryString["ContentTypeCode"];
            if (!string.IsNullOrEmpty(lContentTypeCode.ToString()))
            {
                List<ContentStatistics> lContentStatistics;
                SiteAdminContentManagementBus lSiteAdminContentManagementBus = new SiteAdminContentManagementBus();
                lContentStatistics = lSiteAdminContentManagementBus.GetAllContentMetaDataWithContentStatistics(this.ContentTypeCode);
                TranslateEntityToUIControls(lContentStatistics);
            }

            if (this.ContentTypeCode != "BLG")
            {
                if (this.ContentTypeCode != "DNL")
                {
                    this.lblDisplayListName.Text = "Video Content List";
                }
                else
                {
                    this.lblDisplayListName.Text = "Resource Summary List";
                    
                }
            }
            else
            {
                this.lblDisplayListName.Text = "Blog Summary List";
            }

            if (Session["UserContext"] != null)
            {
                UserContext tempUserSession = (UserContext)Session["UserContext"];
                if (tempUserSession.CurrentUser != null)
                {
                    this.DivCreateNewContent.Visible = true;
                }
            }
        }

        /// <summary>
        /// This method is to copy the entity properties that need to be displayed in the UI controls to the UI control.
        /// Example - [pUser] is an instance of an entity and is filled with specific USER data.
        /// and this user instance is to be displayed is an UI control text boxes.
        /// A sample code will look like the following:
        ///  this.txtFirstName.Text = pUser.UserFirstName; 
        ///  this.txtLastName.Text  = pUser.UserLastName; 
        ///  this.txtLoginId.Text   = pUser.UserLoginID;
        /// </summary>
        private void TranslateEntityToUIControls(List<ContentStatistics> pContentStatistics)
        {
            this.RptContentSummaryControl.DataSource = pContentStatistics;
            this.RptContentSummaryControl.DataBind();
        }

        /// <summary>
        /// This method is to copy the user entered data from the UI controls to the entity instance that will be used to sent the entity to any other layer.
        /// Example - [pUser] is an instance of an entity and is to be filled with specific USER data that has been captured in the screen.
        /// A sample code will look like the following:
        ///  pUser.UserFirstName = this.txtFirstName.Text; 
        ///  pUser.UserLastName  = this.txtLastName.Text; 
        ///  pUser.UserLoginID   = this.txtLoginId.Text; 
        /// </summary>
        private void TranslateUIControlsToEntity()
        {
        }
        #endregion Private Methods

        


        //private void LoadBlogList()
        //{
        //    List<ContentMetaDataMaster> lContentMetaDataMaster;
        //    SiteAdminContentManagementBus lSiteAdminContentManagementBus = new SiteAdminContentManagementBus();
        //    lContentMetaDataMaster = lSiteAdminContentManagementBus.GeAllContentMetaDataByContentTypeKey(1);


        //    this.grdFeedbackList.DataSource = lContentMetaDataMaster;
        //    this.grdFeedbackList.DataBind();
        //}
    }
}